package com.qinggeng.project.system.controller;

import java.math.BigDecimal;
import java.util.List;

import com.qinggeng.common.utils.text.Convert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.project.system.domain.GoodsPercent;
import com.qinggeng.project.system.service.IGoodsPercentService;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.web.page.TableDataInfo;
/**
 * 分类配置Controller
 *
 * @author smallfish
 * @date 2020-11-29
 */
@RestController
@RequestMapping("/system/percent")
public class GoodsPercentController extends BaseController
{

    public static final BigDecimal ZERO=new BigDecimal(0);
    @Autowired
    private IGoodsPercentService goodsPercentService;

    /**
     * 查询分类配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:percent:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsPercent goodsPercent)
    {
        startPage();
        List<GoodsPercent> list = goodsPercentService.selectGoodsPercentList(goodsPercent);
        return getDataTable(list);
    }

    /**
     * 导出分类配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:percent:export')")
    @Log(title = "分类配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsPercent goodsPercent)
    {
        List<GoodsPercent> list = goodsPercentService.selectGoodsPercentList(goodsPercent);
        ExcelUtil<GoodsPercent> util = new ExcelUtil<GoodsPercent>(GoodsPercent.class);
        return util.exportExcel(list, "percent");
    }

    /**
     * 获取分类配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:percent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(goodsPercentService.getGoodsPercentById(id));
    }

    /**
     * 新增分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:percent:add')")
    @Log(title = "分类配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsPercent goodsPercent)
    {
        goodsPercent.setPercent(Convert.strToBigDecimal(goodsPercent.getPercentStr(),ZERO));
        goodsPercent.setProfitRate(Convert.strToBigDecimal(goodsPercent.getProfitRateStr(),ZERO));
        return toAjax(goodsPercentService.insertGoodsPercent(goodsPercent));
    }

    /**
     * 修改分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:percent:edit')")
    @Log(title = "分类配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsPercent goodsPercent)
    {
        goodsPercent.setPercent(Convert.strToBigDecimal(goodsPercent.getPercentStr(),ZERO));
        goodsPercent.setProfitRate(Convert.strToBigDecimal(goodsPercent.getProfitRateStr(),ZERO));
        return toAjax(goodsPercentService.updateGoodsPercent(goodsPercent));
    }

    /**
     * 删除分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:percent:remove')")
    @Log(title = "分类配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsPercentService.deleteGoodsPercentByIds(ids));
    }
}
